//
//  CategoryViewController.m
//  TestYoho
//
//  Created by YiDarren on 17/1/19.
//  Copyright © 2017年 YiDarren. All rights reserved.
//

#import "CategoryViewController.h"
#import "YHNetworkManager.h"


@interface CategoryViewController ()

@end

@implementation CategoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"分类";

    [self getNetData];
    
}



#pragma mark -post方法测试
- (IBAction)postMethodTest:(id)sender {
    
    YHSessionInfo *sessionInfo = [YHSessionInfo new];
    sessionInfo.URLString = @"https://ssrj.com/api/v5/mail/sendMailCode.jhtml";
    sessionInfo.postParamDic = [NSMutableDictionary dictionaryWithDictionary:@{@"email":@"676692975@qq.com",@"type":@"ios",@"smsType":@"findpassword"}];
    
    [[YHNetworkManager sharedInstance] postWithSessionInfo:sessionInfo success:^(NSURLSessionDataTask *task, id responseObject) {
        
        [MBProgressHUD showActivityMessageInView:@"注册邮箱"];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [MBProgressHUD hideHUD];
            
        });

    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
}


- (IBAction)uploadButtonClicked:(id)sender {
    
    YHSessionInfo *sessionInfo = [YHSessionInfo new];
    sessionInfo.URLString = @"/api/v5/member/updateBackgroudImage.jhtml";
    
    [[YHNetworkManager sharedInstance] uploadWithSessionInfo:sessionInfo constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        
        // 上传文件设置
        //NSString *path = [[NSBundle mainBundle] pathForResource:[@"1" stringByAppendingString:@"@2x"] ofType:@"gif"];
        NSString *path = [[NSBundle mainBundle] pathForResource:@"bgbg" ofType:@"png"];
        NSData *data = [NSData dataWithContentsOfFile:path];
        //拼接数据
        /*
         第一个参数:文件参数 (二进制数据)
         第二个参数:参数名~file
         第三个参数:该文件上传到服务器以什么名称来保存
         第四个参数:
         */
        [formData appendPartWithFileData:data name:@"image" fileName:@"test" mimeType:@"image/png"];
        
    } success:^(NSURLSessionDataTask *task, NSError *error) {
        
        [MBProgressHUD showSuccessMessage:@"uploadSuccess"];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        [MBProgressHUD showWarnMessage:@"uploadError"];
        
    }];
    
}


- (void)getNetData {
    
//    __weak typeof(&*self)weakSelf = self;

    YHSessionInfo *sessionInfo = [[YHSessionInfo alloc]init];
    sessionInfo.URLString = @"https://ssrj.com/b180/api/v2/search/hotwords/?pagenum=0&pagesize=10&hotwordstype=0&appVersion=3.0.0&token=227a1368bd09faa8f94d9181710ba533";

    [[YHNetworkManager sharedInstance] getWithSessionInfo:sessionInfo success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if ([responseObject objectForKey:@"state"]) {
            
            NSNumber *state = responseObject[@"state"];
            if (state.intValue == 0) {
                
                [MBProgressHUD showActivityMessageInView:nil];
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    
                    [MBProgressHUD hideHUD];
                    
                });
            }
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        [MBProgressHUD showErrorMessage:[error localizedDescription]];
    }];
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}



@end
